package main

import "github.com/gin-gonic/gin"

func main() {
	r := gin.Default()
	// 测试：http://localhost:8080/welcome?lastname=测试&firstname=666
	r.GET("welcome", welcomeHandler)

	//测试: http://localhost:8080/array?ids=2,3,4,7
	r.GET("array", arrayHandler)

	//测试:  http://localhost:8080/map?user[name]=xiaoming&user[age]=16
	r.GET("map", mapHandler)
	r.Run()
}

func mapHandler(c *gin.Context) {
	m := c.QueryMap("user")
	c.JSON(200, gin.H{
		"data": m,
	})
}

func arrayHandler(c *gin.Context) {
	ids := c.QueryArray("ids")
	c.JSON(200, gin.H{
		"ids": ids,
	})
}

func welcomeHandler(c *gin.Context) {
	firstname := c.DefaultQuery("firstname", "firstDemo")
	lastname := c.Query("lastname")
	c.JSON(200, gin.H{
		"firstname": firstname,
		"lastname":  lastname,
	})
}
